<?php
include ('chek.php');
//删除内容
$pid=$input->get('pid');
if($input->get('pid')){
    $del_sql="DELETE FROM page WHERE pid='{$pid}'";
    $is1=$db->query($del_sql);
    if($is1==true){
        header('location:blog.php');
    }else{
        die('删除失败');
    }
}

//定义每个页面显示数量
$pagenumber=$setting['pagenum'];

//计算数据总量
$sql_total="SELECT COUNT(*) AS total FROM page ";
$page=$db->query($sql_total)->fetch_array(MYSQLI_ASSOC)['total'];
$chuyu=ceil($page/$pagenumber);

//获取并处理当前页面
$page_num=(int) $input->get('page');
$page_num=$page_num<1?1:$page_num;

//计算偏移量
$offset=$pagenumber*($page_num-1);

//输出并执行sql内容
$sql="SELECT * FROM page ORDER by pid desc  limit $offset,{$pagenumber}";
$mysql_result=$db->query($sql);
$rows=array();
while ($is=$mysql_result->fetch_array(MYSQLI_ASSOC)){
    $rows[]=$is;
}
?>
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8"/>
    <title>博客管理</title>
    <?php include (PATH.'/header.inc.php'); ?>

</head>
<body>
<?php include ('./nav.inc.php')?>
<div class="container">
    <div class="row">
        <h1>博客管理<small class="pull-right"><a class="btn btn-default" href="blog_add.php">添加博客</a></small></h1>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>PID</th>
                <th>标题</th>
                <th>作者</th>
                <th>插入时间</th>
                <th>修改时间</th>
                <th>管理</th>
            </tr>
            </thead>
            <tbody>
            <?php
            foreach ($rows as $is) {
                ?>
                <tr>
                    <th scope="row"><?php echo $is['pid']?></th>
                    <th scope="row"><?php echo $is['title']?></th>
                    <th scope="row"><?php echo $is['aptor']?></th>
                    <th scope="row"><?php echo date('y-m-d H:s:i',$is['intime'])?></th>
                    <th scope="row"><?php echo date('y-m-d H:s:i',$is['uptime'])?></th>
                    <td>
                        <a href="blog_add.php?pid=<?php echo $is['pid']?>">修改</a>
                        <a onclick="return confirm('确定删除吗？')" href="blog.php?do=delete&pid=<?php echo $is['pid']?>">删除</a>
                    </td>
                </tr>
                <?php
            }
            ?>
            </tbody>
        </table>
        <!-- 分页 -->
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <?php
                $href="<li><a href='blog.php?page=%d'>%s</a></li>";
                for($i=1;$i<=$chuyu;$i++){
                    echo sprintf($href,$i,"第{$i}页");
                }
                ?>
            </ul>
        </nav>
    </div>
</div>
</body>
</html>
